package com.it.controller;


import com.it.pojo.Book;
import com.it.service.BookService;

import com.it.utils.PageUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/queryByPage")
    public String queryByPage(HttpServletRequest request,String bookNames,String minPriceKey,String maxPriceKey,String curPageKey,String pageSizeKey){
        if(bookNames==null){
            bookNames="";
        }
        Double minPrice=null;
        if(minPriceKey!=null&&minPriceKey.length()>0){
            minPrice=Double.parseDouble(minPriceKey);
        }
        Double maxPrice=null;
        if(maxPriceKey!=null&&maxPriceKey.length()>0){
            maxPrice=Double.parseDouble(maxPriceKey);
        }
        Integer curPage=1;
        if(curPageKey!=null){
            curPage=Integer.parseInt(curPageKey);
        }
        Integer pageSize=0;
        if(pageSizeKey!=null){
            pageSize=Integer.parseInt(pageSizeKey);
        }else {
            pageSize=5;
        }
        Map<String,Object> params=new HashMap<>();
        params.put("bookNameKey","%"+bookNames+"%");
        params.put("minPriceKey",minPrice);
        params.put("maxPriceKey",maxPrice);
        int total = bookService.queryTotal(params);
        PageUtils<Book> pageUtils=new PageUtils<>();
        pageUtils.setTotalCount(total);
        pageUtils.setPageSize(pageSize);
        if(curPage>pageUtils.getTotalPages()){
            curPage=pageUtils.getTotalPages();
        }
        if(curPage<1){
            curPage=1;
        }
        pageUtils.setCurPage(curPage);
        params.put("startIndexKey",(curPage-1)*pageSize);
        params.put("pageSizeKey",pageSize);
        PageUtils<Book> pageUtils1 = bookService.queryByPage(params, pageUtils);
        request.setAttribute("pageUtils1Key",pageUtils1);
        request.setAttribute("bookNames2",bookNames);
        request.setAttribute("minPriceKey2",minPrice==null?"":minPriceKey);
        request.setAttribute("maxPriceKey2",maxPrice==null?"":maxPriceKey);
        return "bookList";
    }
    @RequestMapping("/saves")
    public String saves(){
        return "addBook";
    }
    //新增
    @RequestMapping("/save")
    public String save(HttpServletRequest request,String bookName, String writer, Double price, MultipartFile bookPicture) throws IOException {
        if(bookPicture.isEmpty()){
            request.setAttribute("tip", "你还没有选择文件");
            return "addBook";
        }
        String originalFilename = bookPicture.getOriginalFilename();
        String extName  = originalFilename.substring(originalFilename.lastIndexOf("."));
        String mainName  = UUID.randomUUID().toString().replaceAll("-", "");
        String targetFileName=mainName+extName;
        bookPicture.transferTo(new File("F:/1",targetFileName));
        Book book=new Book(null,bookName,writer,price,targetFileName);
        int row = bookService.save(book);
        return "forward:/book/queryByPage";
    }
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(String bookPicture){
        byte[] bytes = null;
        HttpHeaders headers =null;
        try {
//            读要下载的文件的数据到字节数组中
            bytes = FileUtils.readFileToByteArray(new File("F:/1", bookPicture));
            headers = new HttpHeaders();
//        设置响应体
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//        设置是下载，不是浏览器打开
//            fileName指定了下载下来的文件名称
            headers.setContentDispositionFormData("attachment",bookPicture);
        } catch (IOException e) {
        }
        return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);

    }
   /* //删除
    @RequestMapping("/drop")
    public String drop(Integer id){
        int row = carService.drop(id);
        return "forward:/car/queryByPage";
    }
    //根据id查
    @RequestMapping("/queryById")
    public String queryById(HttpServletRequest request,Integer id){
        Car car = carService.queryById(id);
        System.out.println(car);
        request.setAttribute("carKey",car);
        return "modify";
    }
    //修改
    @RequestMapping("/modify")
    public String modify(Car car){
        int row = carService.modify(car);
        return "forward:/car/queryByPage";
    }
    //批量删除
    @RequestMapping("/dropBatch")
    public String dropBatch(Integer[] id){
        int row = carService.dropBatch(id);
        return "forward:/car/queryByPage";
    }*/
}
